define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    item: {},
                    publish_text: '确认提交',
                    is_publish: 0,
                    type:'member_card',
                    mounted:0,
                    store:{},
                    cardInfo:{
                      base_info:{}
                    },
                    id:0,
                    card_id:0,
                    uploadUrl:app.params.apiDomain+"apiv2/card/uploadimg"
                };
            },
            el: '#pageAddMemberCard',
            created: function() {
                this.fetchData();
            },
            methods: {
                fetchData: function() {
                  var _this = this;
                  this.appid =this.$route.params.appid;
                  this.id = this.$route.params.id;
                  // console.log(this.id);
                  if(this.id>0){
                     this.getCardInfo();
                  }
                  this.getStore();
                },
                getCardInfo:function(){
                   var _this = this;
                    var userInfo = app.getUserInfo();
                    app.get("apiv2/wx-card/find",{id:this.id},function(res){
                        // alert(123);
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        _this.card_id = res.data.cardId;
                        _this.appid = res.data.appid;
                        _this.cardInfo = res.data.config;  
                    });
                },
                
                getStore:function(){
                    var _this = this;
                    var userInfo = app.getUserInfo();
                    app.get("api/user/info",{fields:'store'},function(res){
                        // alert(123);
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        _this.store = res.data.store;  
                    });
                },
                
                publish: function() {
                    var _this = this;
                    if (_this.is_publish === 1) {
                        return;
                    }

                     _this.is_publish = 1;
                    // this.showLimitTimeTex = showText;
                    // this.time_limit = time_limit;
                    // this.date_info = data;
                    var data = $("#pageAddMemberCard").find("#addCardForm").serializeArray();
                   
                     data.push({
                        name:"appid",
                        value: this.appid
                    });
                     data.push({
                        name:"card_id",
                        value: this.card_id
                    });
                     

                    
                    // console.log(data);

                    app.post("apiv2/card/add",data,function(res){
                       _this.is_publish =0;
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                         app.setPageData("memberCard",{reload:1});
                       
                        app.alert("创建成功",function(){
                               _this.$router.go(-2);
                        });

                    });
                    

                },
                
            },
            
             mounted: function() {

                if (this.mounted == 1) {
                    return;
                }
                var _this = this;
                app.upload("#uploadBg",function(){
                     window.photo.uploadUrl = _this.uploadUrl;
                });
              
                this.mounted = 1;

            },

        }).$mount('#app');
    };
    module.exports = controller;
});
